package com.lbssoft.gpsbd.service.impl;

import com.google.common.base.Strings;
import com.lbssoft.gpsbd.common.Utils;
import com.lbssoft.gpsbd.dao.GroupGeofenceMapper;
import com.lbssoft.gpsbd.dao.GroupMapper;
import com.lbssoft.gpsbd.model.Group;
import com.lbssoft.gpsbd.model.GroupGeofence;
import com.lbssoft.gpsbd.model.Result;
import com.lbssoft.gpsbd.model.vo.GroupGeofencesParam;
import com.lbssoft.gpsbd.model.vo.Move;
import com.lbssoft.gpsbd.model.vo.UserVo;
import com.lbssoft.gpsbd.service.GroupService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Set;

/**
 * Created by Riven on 2018/3/15.
 */
@Service
public class GroupServiceImpl implements GroupService {
    @Autowired
    public GroupMapper groupMapper;

    @Autowired
    GroupGeofenceMapper groupGeofenceMapper;

    @Override
    public Result list() {
        UserVo user = (UserVo) SecurityUtils.getSubject().getPrincipal();
        Result result = new Result();
        result.setData(groupMapper.list(user.getId()));
        return result;
    }

    @Override
    public Result add(Group group) {
        UserVo user = (UserVo) SecurityUtils.getSubject().getPrincipal();
        Result result = new Result();
        if(Strings.isNullOrEmpty(group.getUuid())){
            group.setUuid("test-"+ Utils.getUUID().substring(5));
        }
        groupMapper.add(group);
        groupMapper.addUserGroup(user.getId(),group.getId());
        return result;

    }

    @Override
    public Result delete(int id) {
        Result result = new Result();
        groupMapper.delete(id);
        return result;
    }

    @Override
    public Result update(Group group) {
        Result result = new Result();
        groupMapper.update(group);
        return result;
    }

    @Override
    public void move(Move move) {
        groupMapper.move(move);
    }

    @Override
    public Result groupBindGeofences(GroupGeofencesParam param) {
        Result result = new Result();
        //todo 需要同步到采集中心
        for(Integer geofenceId:param.getGeofenceIds()){
            GroupGeofence groupGeofence = new GroupGeofence();
            groupGeofence.setGroupId(param.getGroupId());
            groupGeofence.setGeofenceId(geofenceId);
            groupGeofenceMapper.addGroupGeofence(groupGeofence);
        }
        result.setCode(0);
        return result;
    }

    @Override
    public Result groupUnbindGeofences(GroupGeofencesParam param) {
        Result result = new Result();
        for(Integer geofenceId:param.getGeofenceIds()){
            GroupGeofence groupGeofence = new GroupGeofence();
            groupGeofence.setGroupId(param.getGroupId());
            groupGeofence.setGeofenceId(geofenceId);
            groupGeofenceMapper.delete(groupGeofence);
        }
        result.setCode(0);
        return result;
    }

    @Override
    public Result getGeofenceIds(int id) {
        Result result = new Result();
        Set<Long> geofenceIds = groupGeofenceMapper.getGeofenceIds(id);
        result.setCode(0);
        result.setData(geofenceIds);
        return result;
    }
}
